"""
用于测试web自动化引擎
"""
from pprint import pprint

from _hellchin_webrun.src.core.engine.action_step import ActionStep
# from _hellchin_webrun.src.core.engine.action_step import ActionStep
from _hellchin_webrun.src.core.engine.models import ConfigModel, SuiteModel

setup_step = [
    {
        "desc": "打开浏览器",
        "method": "open_browser",
        "params": {
            "browser_type": "chromium"
        }
    },
    # {
    #     "desc": "打开页面",
    #     "method": "open_url",
    #     "params": {
    #         'url': "https://www.ketangpai.com/#/login"
    #     }
    # },
    # {
    #     "desc": "等待2秒",
    #     "method": "sleep",
    #     "params": {
    #         "timeout": 2000
    #     }
    # }
]

cases = [
    {
        'id': "TC_LOGIN_001",
        'name': "正确用户名密码登录",
        "skip": False,
        "steps": [
            # {
            #     "desc": "打开浏览器",
            #     "method": "open_browser",
            #     "params": {
            #         "browser_type": "chromium"
            #     }
            # },
            {
                "desc": "打开页面",
                "method": "open_url",
                "params": {
                    'url': "https://www.ketangpai.com/#/login"
                }
            },
            {
                "desc": "输入用户名",
                "method": "fill_value",
                "params": {
                    "locator": """//input[@placeholder="请输入邮箱/手机号/账号"]""",
                    "value": "${{username}}"
                }
            },
            {
                "desc": "输入密码",
                "method": "fill_value",
                "params": {
                    "locator": """//input[@placeholder="请输入密码"]""",
                    "value": "${{password}}"
                }
            },
            {
                "desc": "点击登录按钮",
                "method": "click",
                "params": {
                    "locator": '//*[@id="app"]/div[2]/div[2]/div[2]/div/div/div[1]/div[1]/button'
                }
            }
        ]
    },
    {
        'id': "TC_LOGIN_002",
        'name': "正确用户名密码登录",
        "skip": False,
        "steps": [
            {
                "desc": "重制浏览器上下文",
                "method": "reset_browser_context",
                "params": {}
            },
            {
                "desc": "打开页面",
                "method": "open_url",
                "params": {
                    'url': "/#/login"
                }
            },
            {
                "desc": "输入用户名",
                "method": "fill_value",
                "params": {
                    "locator": """//input[@placeholder="请输入邮箱/手机号/账号"]""",
                    "value": "${{username}}"
                }
            },
            {
                "desc": "输入密码",
                "method": "fill_value",
                "params": {
                    "locator": """//input[@placeholder="请输入密码"]""",
                    "value": "${{password}}"
                }
            },
            {
                "desc": "点击登录按钮",
                "method": "click",
                "params": {
                    "locator": '//*[@id="app"]/div[2]/div[2]/div[2]/div/div/div[1]/div[1]/button'
                }
            }
        ]
    },
    {
        'id': "编号3",
        'name': "关键字测试登录成功",
        "skip": False,
        "steps": [
            {
                "desc": "等待1秒",
                "keyword": "强制等待",
                "params": {
                    "timeout": 1000
                }
            },
            {
                "desc": "输入账号",
                "keyword": "输入内容",
                "params": {
                    'locator': '//input[@placeholder="请输入邮箱/手机号/账号"]',
                    'value': "${{username}}"
                }
            },
            {
                "desc": "等待1秒",
                "keyword": "强制等待",
                "params": {
                    "timeout": 1000
                }
            },
            {
                "desc": "输入密码",
                "keyword": "输入内容",
                "params": {
                    'locator': '//input[@placeholder="请输入密码"]',
                    'value': "${{password}}"
                }
            },
            {
                "desc": "点击登录",
                "keyword": "点击元素",
                "params": {
                    "locator": '//div[@class="margin-top"]/button'
                }
            },
            # {
            #     "desc": "断言是否跳转登录成功后的页面",
            #     "keyword": "断言页面url地址",
            #     "params": {
            #         "expect_results": "https://www.ketangpai.com/#/bindwechat"
            #     }
            # }
            {
                "desc": "动态注册关键字测试",
                "keyword": "动态注册关键字",
            }

        ]
    }

]

suite_data = SuiteModel(
    id="TS_LOGIN_001",
    name="登录用例集合",
    setup_step=setup_step,
    cases=cases
)

env_config = ConfigModel(
    is_debug=True,
    browser_type="chromium",
    host="https://www.ketangpai.com",
    global_variable={
        "username": "python123",
        "password": "123qwe123"
    }
)

key = {
    "name": "动态注册关键字",
    "method": 'def dynamic_register_test():print(\"hello world\")'
}

if __name__ == '__main__':
    from _hellchin_webrun.src.core.engine.runner import Runner
    from _hellchin_webrun.src.core.utils.chinese_alias import KeyWordManage

    # 动态注册关键字
    KeyWordManage.action_class = ActionStep
    KeyWordManage.register_keyword(key.get("name"), key.get("method"))

    # 初始化执行器,传入配置和测试集合,运行测试
    runner = Runner(config=env_config, suite=suite_data)
    pprint(runner.run())
    # test = env_config.model_dump_json()
    # print(test)
    # print(type(test))
